package study.springcloud.client.rest.support.rest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Slf4j
@Configuration
public class RestTemplateConfig {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        //全局超时
        RestTemplate restTemplate = builder
//                .setConnectTimeout(Duration.ofSeconds(6)) //连接超时
//                .setReadTimeout(Duration.ofSeconds(6))    //读取超时
                .additionalInterceptors(new TraceInterceptor())
                .requestFactory(OkHttp3ClientHttpRequestFactory.class)
                .build();
        return restTemplate;
//        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
    }
}
